package com.yq.homework.day281;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author 3307235355@qq.com
 * @description
 * @since 2022/06/30 21:44
 */
public class Demo05 {
    public static void main(String[] args) {
        File file=new File("xx.txt");
        Timer timer = new Timer();
        timer.schedule(new TimetoFile(file),3000,5000);
    }
}
class TimetoFile extends TimerTask {
    File file;

    public TimetoFile(File file) {
        this.file = file;
    }

    @Override
    public void run() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String format = formatter.format(date);
        System.out.println(format);
        try {
            FileWriter out = new FileWriter(file, true);
            out.write(format);
            out.write("\r\n");
            out.flush();
            System.out.println("写入一次");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}